# grid = [
#   ["1","1","1","1","0"],
#   ["1","1","0","1","0"],
#   ["1","1","0","0","0"],
#   ["0","0","0","0","0"]
# ]
grid = [
  ["1","1","0","0","0"],
  ["1","1","0","0","0"],
  ["0","0","1","0","0"],
  ["0","0","0","1","1"]
]

direction=[-1,0,1,0,-1]
def dfs(grid,x,y,rowLen,colLen):
    #出口条件
    if grid[x][y]!="1":
        return None
    #改变当前岛屿状态为已读状态
    grid[x][y]='-1'
    for k in range(4):
        r,c=x+direction[k],y+direction[k+1]
        if r>=0 and r<rowLen and c>=0 and c<colLen:
            dfs(grid,r,c,rowLen,colLen)

def numIslands(grid):
    rowLen=len(grid)
    colLen=len(grid[0])
    # state=[[]]
    #记录岛屿数量的变量
    count=0
    for i in range(rowLen):
        for j in range(colLen):
            if grid[i][j]=="1":
                dfs(grid,i,j,rowLen,colLen)
                count+=1
    print(grid)
    print(count)
numIslands(grid)